package com.example.demospringmybatisplus.processors;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demospringmybatisplus.enums.TableNameServiceEnum;
import com.example.demospringmybatisplus.processInterface.IDataOperationServices;
import com.example.demospringmybatisplus.processInterface.IDataProcessor;
import com.example.demospringmybatisplus.vo.BaseDataVo;
import com.example.demospringmybatisplus.vo.SearchVo;

/**
 * DataOperationService业务处理service层
 *
 * @author
 * @DATE 2024/5/3
 **/
@Service
public class DataOperationService implements IDataOperationServices {

    @Autowired
    private Map<String, IDataProcessor> dataProcessMap;

    // 这里根据tableName获取执行不同的processor
    @Override
    public List<? extends BaseDataVo> findList(SearchVo searchVo, String tableName) {
        return Optional.ofNullable(tableName)
                .filter(name -> !name.isEmpty())
                .map(this::getDataProcessor)
                .map(processor -> processor.findList(searchVo, tableName))
                .orElse(null);
    }

    // 这里根据tableName获取获取不同的processor
    private IDataProcessor getDataProcessor(String tableName) {
        String serviceName = TableNameServiceEnum.getServiceName(tableName);
        return dataProcessMap.get(serviceName);
    }
}
